﻿using System;
using System.Windows.Forms;
using DbVisualizer.DbConnection.MRU;

namespace DbVisualizer.DbConnection.UI
{
    public partial class QueryForm
    {
        private readonly IDbConnectionProxy source;

        private MruList recentList;

        public QueryForm(IDbConnectionProxy source)
        {
            InitializeComponent();

            this.source = source;
        }


        private void Execute()
        {
            var result = source.RunQuery(QueryTextBox.Text);

            if (result.Exception == null)
            {
                ResultGrid.DataSource = result.ResultTable;
                MessageLabel.ForeColor = System.Drawing.Color.Black;
                MessageLabel.Text = string.Format(
                    "{0} {1} returned in {2}ms:",
                    result.ResultTable.Rows.Count,
                    (result.ResultTable.Rows.Count == 1) ? "row" : "rows",
                    result.QueryTime.TotalMilliseconds);


                recentList.Add(QueryTextBox.Text);

                PopulateAutoCompleteList();
            }
            else
            {
                ResultGrid.DataSource = null;
                MessageLabel.ForeColor = System.Drawing.Color.Red;
                MessageLabel.Text = result.Exception.Message.Replace(Environment.NewLine, " - ");
            }
        }


        protected override void OnLoad(EventArgs e)
        {
            QueryTextBox.SelectionStart = QueryTextBox.Text.Length;

            PopulateAutoCompleteList();
            base.OnLoad(e);
        }

        protected override void OnClosed(EventArgs e)
        {
            try
            {
                recentList.Save();
            }
            catch
            {
            }

            base.OnClosed(e);
        }

        private void PopulateAutoCompleteList()
        {
            try
            {
                var connString = source.ConnectionString;
                recentList = MruList.GetMruList(connString);

                var autocompleteList = new AutoCompleteStringCollection();

                foreach (var sql in recentList)
                {
                    autocompleteList.Add(sql);
                }

                QueryTextBox.AutoCompleteCustomSource = autocompleteList;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to populate autocomplete list: " + ex.Message,
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
        }

        private void CloseButtonClick(object sender, EventArgs e)
        {
            Close();
        }

        private void ExecuteButtonClick(object sender, EventArgs e)
        {
            ExecuteButton.Focus();
            Execute();
            QueryTextBox.Focus();
        }
    }
}